/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class Test_trg_AmendmentTrigger {

    static testMethod void myUnitTest()
    {
        // TO DO: implement unit test
        System.debug('The Test Start...');
        Location__c locationObject2 = new Location__c();
        locationObject2.Name = 'Test Land';
        locationObject2.Location_Type__c = 'Land';
        locationObject2.Rental_Rate__c = 18000;
        locationObject2.Number_of_Keys__c = '2';
        insert locationObject2;
       
        //For Legal Status...
        Legal_Status__c legalStatus1 = new Legal_Status__c();
        legalStatus1.Active__c = true;
        legalStatus1.Name = 'Free Zone Entity';
        legalStatus1.Short_Name_English__c = 'F.Z.E';
        insert legalStatus1;
        
        Legal_Status__c legalStatus2 = new Legal_Status__c();
        legalStatus2.Active__c = true;
        legalStatus2.Name = 'Free Zone Company';
        legalStatus2.Short_Name_English__c = 'F.Z.C';
        insert legalStatus2;
        
        //Account Object Record...
        Account accountObject = new Account();
        accountObject.Name = 'Test Account';
        accountObject.License_Application_Step__c = 'Record Created';
        accountObject.License_Application_Type__c = 'New License';
        accountObject.Activity__c = 'Trading - One Activity';
        accountObject.License_Issue_Date__c = system.today();
        accountObject.License_Expiry_Date__c = System.Today().addYears(1);
        accountObject.Master_Account_Flag__c = true;
        accountObject.LegalStatus__c = legalStatus2.Id;
        insert accountObject;
        
        Contact contactObject1 = new Contact();
        contactObject1.LastName = 'Test 2 Shareholder';
        contactObject1.AccountId = accountObject.id;
        contactObject1.Contact_Type__c = 'Shareholder';
        insert contactObject1;
                      
        Contact contactObject2 = new Contact();
        contactObject2.LastName = 'Test 2 Shareholder';
        contactObject2.AccountId = accountObject.id;
        contactObject2.Contact_Type__c = 'Shareholder';
        insert contactObject2;
        
        Share_Ownership__c shareOwnerObject1 = new Share_Ownership__c();
        shareOwnerObject1.Company__c = accountObject.Id;
        shareOwnerObject1.Ownership__c = 50;
        shareOwnerObject1.Share_Profit__c = 50;
        shareOwnerObject1.Shareholder__c = contactObject2.id;
        insert shareOwnerObject1;
        
        Share_Ownership__c shareOwnerObject2 = new Share_Ownership__c();
        shareOwnerObject2.Company__c = accountObject.Id;
        shareOwnerObject2.Ownership__c = 50;
        shareOwnerObject2.Share_Profit__c = 50;
        shareOwnerObject2.Shareholder__c = contactObject2.id;
        insert shareOwnerObject2;
        
       
        System.debug('The Account is Created => '+ accountObject.Id);
        //For Creating the Amendment Record...
        List<Amendment__c> amendmentObjectInsertionList = new List<Amendment__c>();
        List<Amendment__c> amendmentObjectUpdationList = new List<Amendment__c>();
        Amendment__c amendmentObject = null;
        Map<String,RecordType> recordTypeMap = new Map<string,RecordType>();
        System.debug('Start Query to the Amendment Record Types');
        
        //Query for the Record types of Amendment Object...
        for(RecordType recordType : [Select Id,SObjectType,Name,DeveloperName from RecordType where SObjectType =: 'Amendment__c' AND IsActive =: true])
        {   
            recordTypeMap.put(recordType.DeveloperName,recordType);
            System.debug('The Record Type Name => '+ recordType.Name);
            amendmentObject = new Amendment__c();
            amendmentObject.RecordTypeId = recordType.id;
            amendmentObject.Amendment_Application_Status__c = 'New Key Process Initiated';
            amendmentObject.New_Company_Name__c = 'Test New Name';
            amendmentObject.New_Company_Name_Arabic__c = 'test New Name Arabic';    
            amendmentObject.Account__c = accountObject.Id;
            
            //Old Approach before the Confirm Amendment Butto...
            /*if(recordType.Name == 'Location Amendment - New Key')
            {
                if(locationObject2.Number_of_Keys__c != '')
                    amendmentObject.Current_of_Keys__c = integer.valueOf(locationObject2.Number_of_Keys__c);
                amendmentObject.Amendment_Application_Status__c = 'New Key Process Initiated';
                
                amendmentObject.Location_ForKey__c = locationObject2.Id;
                System.debug('The Inside Location Amendment - New Key After Object Created after location');
                
                amendmentObject.Keys_Required__c = '2';
            
            }*/
            
            //Adding in the List...
            amendmentObjectInsertionList.add(amendmentObject);
            
           
        }
        System.debug('Before Inserted Amendments => '+ amendmentObjectInsertionList.size());
        //First Insert the Amendment...
        if(amendmentObjectInsertionList.size() > 0)
            insert amendmentObjectInsertionList;
        
        System.debug('The Inserted Amendment Records List => '+ amendmentObjectInsertionList);
        //User userObject = [Select id from User Limit 1];
        System.debug('The Current User =>'+ USERINFO.getUserId());  
        for(Amendment__c amendmentObject1 : amendmentObjectInsertionList)
        {
            //if(amendmentObject1.recordTypeId == recordTypeMap.get('Shareholder_Amendment_Change_Share_Percentage').Id)
            if(amendmentObject1.recordTypeId == recordTypeMap.get('Shareholder_Amendment_Replace_Partner').Id)
            {
                System.debug('Inside the Record Type Shareholder_Amendment_Change_Share_Percentage => '+ amendmentObject1.Record_Type_Name_Formula__c); 
                //amendmentObject1.Amendment_Application_Status__c = 'Approved By BackOffice Department';
                amendmentObject1.Amendment_Application_Status__c = 'Ready for Processing Chamber Certificate';
                amendmentObject1.Record_Approver_Id__c = USERINFO.getUserId();
                //For ShareHolder Amendment Object Add Flag...
                Shareholder_Amendment__c shareAmendmentObject = new Shareholder_Amendment__c();
                shareAmendmentObject.Account__c = accountObject.id;
                shareAmendmentObject.Amendment__c = amendmentObject1.Id;
                shareAmendmentObject.Add_Flag__c = true; 
                shareAmendmentObject.Ownership__c = 10;
                shareAmendmentObject.Share_Profit__c = 10;
                insert shareAmendmentObject;
                
                //For ShareHolder Amendment Object Edit Flag...
                Shareholder_Amendment__c shareAmendmentObject1 = new Shareholder_Amendment__c();
                shareAmendmentObject1.Account__c = accountObject.id;
                shareAmendmentObject1.Contact__c = contactObject1.id;
                shareAmendmentObject1.Share_Ownership__c = shareOwnerObject1.id;
                shareAmendmentObject1.Amendment__c = amendmentObject1.Id;
                shareAmendmentObject1.Edit_Flag__c = true; 
                shareAmendmentObject1.Ownership__c = 40;
                shareAmendmentObject1.Share_Profit__c = 40;
                insert shareAmendmentObject1;
                
                //For ShareHolder Amendment Object Delete Flag...
                Shareholder_Amendment__c shareAmendmentObject2 = new Shareholder_Amendment__c();
                shareAmendmentObject2.Account__c = accountObject.id;
                shareAmendmentObject2.Contact__c = contactObject2.id;
                shareAmendmentObject2.Share_Ownership__c = shareOwnerObject2.id;
                shareAmendmentObject2.Amendment__c = amendmentObject1.Id;
                shareAmendmentObject2.Delete_Flag__c = true; 
                shareAmendmentObject2.Ownership__c = 50;
                shareAmendmentObject2.Share_Profit__c = 50;
                insert shareAmendmentObject2;
                
            }
            else if(amendmentObject1.recordTypeId == recordTypeMap.get('Company_Amendment_Change_Company_Name').Id)
            {
            	amendmentObject1.Amendment_Application_Status__c = 'Ready for Processing Chamber Certificate';
                amendmentObject1.Record_Approver_Id__c = USERINFO.getUserId();
                amendmentObject1.New_Company_Name__c = 'Testting';
           		amendmentObject1.New_Company_Name_Arabic__c = 'Testing Arabic';
           		
            }
            else
            {
                amendmentObject1.Record_Approver_Id__c = USERINFO.getUserId();
                amendmentObject1.Amendment_Application_Status__c = 'Submitted By Archive Department';
           		
            }
                    
            //Update in the list...
            amendmentObjectUpdationList.add(amendmentObject1);
        }
        
        //First Update the Amendment...
        if(amendmentObjectUpdationList.size() > 0)
            update amendmentObjectUpdationList;
        
    }
}